
window.onload=function (){
	var data=localStorage.getItem('search');
	var text=document.getElementsByTagName('input')[0];
	var hidden=document.getElementsByClassName('searchlist')[0];
	var btn=document.getElementsByClassName('btn')[0];
	var cur = -1;
	data=data?JSON.parse(data):[];

	window.btn_click = function(){
		var txt=text.value;
		if(data.indexOf(txt)==-1){
			data.push(txt);
		}
		localStorage.setItem('search',JSON.stringify(data));
		text.value='';
		hidden.className='hidden';
		window.open('http://search.tongwei.cn/index?keyword='+txt,'_self'); 
	};
	
	window.del_click =function(){
		 localStorage.clear();
		 location.reload('search');
	}
	
	// 聚焦显示
	window.onAjaxlist =function(e){
		var txt=text.value;
		var html='';
		for(var i=0;i<data.length;i++){
			var reg=new RegExp(txt); 
			var index= data[i].search(reg);
			if(index!=-1){
				html+=' <li>'+data[i]+'</li>';
			}
		}
		hidden.innerHTML=html;
		hidden.className='show';
		
		var as = document.getElementById('searchlist').getElementsByTagName('li');
		if(as.length>0){
			document.getElementById('more').className='showplus';
		}
	}
	
	// 键盘操作
	window.checkKeyDown =function(e){
		var as = document.getElementById('searchlist').getElementsByTagName('li');
		switch(e.keyCode) {
			case 38: //上翻
				if(cur == -1) cur = as.length - 1;
				else {
					as[cur].className = '';
					cur -= 1;
				}
				if(cur < 0) cur = as.length - 1;
				if(as.length>0){
					as[cur].classList.add("on");
				}
				break;
			case 40: //下翻
				if(cur == -1) {
					cur = 0;
				}
				else {
					as[cur].className = '';
					cur++;
				}
				if(cur >= as.length) {
					cur = 0;
				}
				if(as.length>0){
					as[cur].classList.add("on");
				}
				break;
			case 13: //回车
				if(cur != -1) {
					var txt = as[cur].innerHTML;
					window.open('http://search.tongwei.cn/index?keyword='+txt,'_self');
				}
				else{
					var txt=text.value;
					if(data.indexOf(txt)==-1){
						data.push(txt);
					}
					localStorage.setItem('search',JSON.stringify(data));
					text.value='';
					document.getElementById('keywords').value =txt;
					hidden.className='hidden';
				window.open('http://search.tongwei.cn/index?keyword='+txt,'_self');
				}
				break;
		}
	}
	
	// 关键词联动
	window.checkKeyUp =function(event){
		var txt=text.value;
		var html='';
		for(var i=0;i<data.length;i++){
			var reg=new RegExp(txt); 
			var index= data[i].search(reg);
			if(index!=-1){
				html+=' <li>'+data[i]+'</li>';
			}
		}
		hidden.innerHTML=html;
		hidden.className='show';
		document.getElementById('more').className='hiddenplus';
		var as = document.getElementById('searchlist').getElementsByTagName('li');
		if(cur >= as.length) {
			cur = -1;
		}
		if(as && as.length>1){
			as[cur].classList.add("on");
		}
	};
	
	window.liclick =function(e){
		var li= event.target;
		var title=li.innerHTML;
		text.value=title;
		window.open('http://search.tongwei.cn/index?keyword='+title,'_self'); 
	}
	
	// 失焦隐藏
	window.offAjaxlist =function(e){
		setTimeout(() => {
			document.getElementById('more').className='hiddenplus';
			hidden.className='hidden';
		}, 300);
			
	}

}



